#include "engine.h"
#include "Windows.h"
Engine::Engine(MainWindow * w)
{
    cb = &w->cb_with_num;
    player = &w->cb_with_num.player;
    hash_manger = new HashManger();
    hash_manger->setParent(this);
    connect(w,&MainWindow::computer_first,[this](){
        qDebug() << "first";
        this->computer_remote = false;
    });
    connect(w,&MainWindow::computer_remote,[this](){
        qDebug() << "remote";
        this->computer_remote = true;
    });
    connect(w,&MainWindow::stop,this,&Engine::stop);
}

void Engine::reset_hash(bool is_first){
    if(if_use_hash){
        while(!hash_manger->is_finished()) Sleep(100);
        hash_manger->reset_hash();
        if(is_first) hash_manger->load_future(8,get_depth(8));
        else hash_manger->load_future(7,get_depth(7));
    }
}

Score_struct Engine::find_score(Cb_with_num &cb){
    auto s = hash_manger->find_score(cb);
    if(s.is_valid()) return s;
    else{
        Score_struct s0(king_move(cb),queen_move(cb));
        if(if_update_hash)  hash_manger->update(cb,s0);
        return s0;
    }
}

